/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.glidebitmappoolsample.slice;

import com.glidebitmappool.GlideBitmapFactory;
import com.glidebitmappool.GlideBitmapPool;

import com.glidebitmappoolsample.Constants;
import com.glidebitmappoolsample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Environment;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.File;

/**
 * 主页
 *
 * @author renpengfei
 * @since 2021-03-12
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP, 0x001, "TAG");

    /**
     * 主页启动
     *
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initComponent();
    }

    private void initComponent() {
        Button normalResource = (Button) findComponentById(ResourceTable.Id_normalResource);
        Button normalFile = (Button) findComponentById(ResourceTable.Id_normalFile);
        Button resourceOptimized = (Button) findComponentById(ResourceTable.Id_resourceOptimized);
        normalResource.setClickedListener(this);
        normalFile.setClickedListener(this);
        resourceOptimized.setClickedListener(this);

        Button fileOptimized = (Button) findComponentById(ResourceTable.Id_fileOptimized);
        Button downSample = (Button) findComponentById(ResourceTable.Id_downSample);
        Button clearMemory = (Button) findComponentById(ResourceTable.Id_clearMemory);
        fileOptimized.setClickedListener(this);
        downSample.setClickedListener(this);
        clearMemory.setClickedListener(this);
    }

    /**
     * 正常资源回收机制
     */
    private void runNormalResource() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                releaseBitmapMethod();
            }
        }).start();
    }

    private void releaseBitmapMethod() {
        HiLog.error(logLabel, "runNormalResource---start");
        ImageSource imageSource = ImageSource.createIncrementalSource(new ImageSource.SourceOptions());
        PixelMap pixelMap1 = imageSource.createPixelmap(
                ResourceTable.Media_test1, GlideBitmapFactory.getDecodingOptions());
        pixelMap1.release();
        PixelMap pixelMap2 = imageSource.createPixelmap(
                ResourceTable.Media_test2,GlideBitmapFactory.getDecodingOptions());
        pixelMap2.release();
        PixelMap pixelMap3 = imageSource.createPixelmap(
                ResourceTable.Media_test3,GlideBitmapFactory.getDecodingOptions());
        pixelMap3.release();
        PixelMap pixelMap4 = imageSource.createPixelmap(
                ResourceTable.Media_test4,GlideBitmapFactory.getDecodingOptions());
        pixelMap4.release();
        PixelMap pixelMap5 = imageSource.createPixelmap(
                ResourceTable.Media_test5,GlideBitmapFactory.getDecodingOptions());
        pixelMap5.release();
        PixelMap pixelMap6 = imageSource.createPixelmap(
                ResourceTable.Media_test6,GlideBitmapFactory.getDecodingOptions());
        pixelMap6.release();
        PixelMap pixelMap7 = imageSource.createPixelmap(
                ResourceTable.Media_test7,GlideBitmapFactory.getDecodingOptions());
        pixelMap7.release();
        PixelMap pixelMap8 = imageSource.createPixelmap(
                ResourceTable.Media_test8,GlideBitmapFactory.getDecodingOptions());
        pixelMap8.release();
        PixelMap pixelMap9 = imageSource.createPixelmap(
                ResourceTable.Media_test9,GlideBitmapFactory.getDecodingOptions());
        pixelMap9.release();
        PixelMap pixelMap10 = imageSource.createPixelmap(
                ResourceTable.Media_test10,GlideBitmapFactory.getDecodingOptions());
        pixelMap10.release();
        HiLog.error(logLabel, "runNormalResource---end");
    }

    /**
     * 普通文件回收机制
     */
    private void runNormalFile() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HiLog.error(logLabel, "runNormalFile---start");
                String path = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath()
                        + File.separator + Constants.TEST;
                HiLog.error(logLabel, "runNormalFile--path--" + path);
                for (int i1 = 1; i1 <= Constants.TEN; i1++) {
                    ImageSource imageSource = ImageSource.create(
                            path + i1 + Constants.FORMAT,new ImageSource.SourceOptions());
                    PixelMap pixelMap = imageSource.createPixelmap(GlideBitmapFactory.getDecodingOptions());
                    pixelMap.release();
                }
                HiLog.error(logLabel, "runNormalFile---end");
            }
        }).start();
    }

    /**
     * 资源优化缓存
     */
    private void runResourceOptimized() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                putBitmapMethod();
            }
        }).start();
    }

    private void putBitmapMethod() {
        HiLog.error(logLabel,"runResourceOptimized---start");
        PixelMap pixelMap1 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test1);
        GlideBitmapPool.putBitmap(pixelMap1);
        PixelMap pixelMap2 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test2);
        GlideBitmapPool.putBitmap(pixelMap2);
        PixelMap pixelMap3 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test3);
        GlideBitmapPool.putBitmap(pixelMap3);
        PixelMap pixelMap4 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test4);
        GlideBitmapPool.putBitmap(pixelMap4);
        PixelMap pixelMap5 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test5);
        GlideBitmapPool.putBitmap(pixelMap5);
        PixelMap pixelMap6 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test6);
        GlideBitmapPool.putBitmap(pixelMap6);
        PixelMap pixelMap7 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test7);
        GlideBitmapPool.putBitmap(pixelMap7);
        PixelMap pixelMap8 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test8);
        GlideBitmapPool.putBitmap(pixelMap8);
        PixelMap pixelMap9 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test9);
        GlideBitmapPool.putBitmap(pixelMap9);
        PixelMap pixelMap10 = GlideBitmapFactory.decodeResource(getContext(), ResourceTable.Media_test10);
        GlideBitmapPool.putBitmap(pixelMap10);
    }

    /**
     * 文件优化缓存
     */
    private void runFileOptimized() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HiLog.error(logLabel, "runFileOptimized---start");
                    String path = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath()
                            + File.separator + Constants.TEST;
                    HiLog.error(logLabel, "runFileOptimized--path--" + path);
                    for (int i1 = 1; i1 <= Constants.TEN; i1++) {
                        PixelMap pixelMap = GlideBitmapFactory.decodeFile(path + i1 + Constants.FORMAT);
                        GlideBitmapPool.putBitmap(pixelMap);
                    }
                    HiLog.error(logLabel, "runFileOptimized---end");
                } catch (IllegalArgumentException e) {
                    HiLog.error(logLabel, e.getMessage());
                }
            }
        }).start();
    }

    /**
     * 文件指定大小优化缓存
     */
    private void runDownSample() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HiLog.error(logLabel, "runDownSample---start");
                    String absolutePath = getContext().getExternalCacheDir().getAbsolutePath();
                    String path = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath()
                            + File.separator + Constants.TEST;
                    for (int i1 = 1; i1 <= Constants.TEN; i1++) {
                        PixelMap pixelMap = GlideBitmapFactory.decodeFile(
                                path + i1 + Constants.FORMAT, Constants.ONE_HUNDRED, Constants.ONE_HUNDRED);
                        GlideBitmapPool.putBitmap(pixelMap);
                    }
                    HiLog.error(logLabel, "runDownSample---end");
                } catch (IllegalArgumentException e) {
                    HiLog.error(logLabel, e.getMessage());
                }
            }
        }).start();
    }

    /**
     * 清除缓存
     */
    private void clearMemory() {
        GlideBitmapPool.clearMemory();
        HiLog.error(logLabel, "clearMemory");
        new ToastDialog(getContext()).setText("clearMemory").setAlignment(LayoutAlignment.CENTER).show();
    }

    /**
     * button 事件监听
     *
     * @param component
     */
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_normalFile:
                runNormalFile();
                break;
            case ResourceTable.Id_normalResource:
                runNormalResource();
                break;
            case ResourceTable.Id_resourceOptimized:
                runResourceOptimized();
                break;
            case ResourceTable.Id_fileOptimized:
                runFileOptimized();
                break;
            case ResourceTable.Id_downSample:
                runDownSample();
                break;
            case ResourceTable.Id_clearMemory:
                clearMemory();
                break;
            default:
                break;
        }
    }
}